
Strict

Class TVector2

	Public
	'-----------------------------------------------------------------------------------
	
	Field x:Float
	Field y:Float 

	Const Zero:TVector2 = New TVector2(0,0)
	
	Method New(v:TVector2)
		Self.x = v.x
		Self.y = v.y
	End
	
	Method New(x:Float=0,y:Float=0)
		Self.x = x
		Self.y = y
	End
	
	Method Add:Void(x:Float,y:Float)
		Self.x += x
		Self.y += y
	End Method
	
	Method Add:Void(v:TVector2)
		Self.x += v.x
		Self.y += v.y
	End Method
	
	Method Sub:Void(x:Float,y:Float)
		Self.x -= x
		Self.y -= y
	End Method
	
	Method Sub:Void(v:TVector2)
		Self.x -= v.x
		Self.y -= v.y
	End Method
	
	Method Mult:Void(x:Float,y:Float)
		Self.x *= x
		Self.y *= y
	End Method
	
	Method Mult:Void(v:TVector2)
		Self.x *= v.x
		Self.y *= v.y
	End Method
	
	Method Mult:Void(f:Float)
		Self.x *= f
		Self.y *= f
	End Method
	
	Method Div:Void(x:Float,y:Float)
		Self.x /= x
		Self.y /= y
	End Method
	
	Method Div:Void(v:TVector2)
		Self.x /= v.x
		Self.y /= v.y
	End Method
	
	'-----------------------------------------------------------------------------------
	' 		S E T
	'-----------------------------------------------------------------------------------
	Method Set:TVector2( vector:TVector2 ) 
		Return Self.Set(vector.x,vector.y)
	End
	
	
	'-----------------------------------------------------------------------------------
	' 		Compare 2 vectors
	'-----------------------------------------------------------------------------------
	Method Compare:Bool( vector:TVector2 ) 
		Return (Self.x=vector.x) And (Self.y = vector.y)
	End
	
	'-----------------------------------------------------------------------------------
	' 		SET
	'-----------------------------------------------------------------------------------
	
	Method Set:TVector2(x:Float,y:Float ) 
		Self.x = x
		Self.y = y
		Return Self	
	End
	
	'-----------------------------------------------------------------------------------
	' 		COPY
	'-----------------------------------------------------------------------------------
	
	Method Copy:TVector2() Property
		Local vector:TVector2 = New TVector2
		vector.x = Self.x
		vector.y = Self.y
		Return vector	
	End
	
	
	'-----------------------------------------------------------------------------------
	'   	G E T   L E N G T H
	'-----------------------------------------------------------------------------------
	' Get current Length of vector, uses a single sqr operation
	'-----------------------------------------------------------------------------------
	
	Method Length:Float() 
		Return Sqrt( Self.x*Self.x + Self.y*Self.y )'
	End

	'-----------------------------------------------------------------------------------
	' D I S T A N C E
	'-----------------------------------------------------------------------------------
	' The Distance between the two points
	' This is NOT related to the vectors Length#	
	'-----------------------------------------------------------------------------------
	
	Method DistanceTo:Float(Other:TVector2) 
		Return DistanceTo(Other.x, Other.y)
	End
	
	Method DistanceTo:Float(x:Float, y:Float) 
		Local dx:Float = Self.x - x 
		Local dy:Float = Self.y - y 
		Return Sqrt( dx*dx + dy*dy ) 	
	End
	
	'-----------------------------------------------------------------------------------
	' 		A N G L E   T O   C W 
	'-----------------------------------------------------------------------------------
	' If you have two vectors so they both start at the same position
	' returns the angle from this vector to target vector (in degrees)
	' if you where to go Clockwise to it, result is 0 to 360	
	'-----------------------------------------------------------------------------------
	
	Method AngleToClockwise:Float( target:TVector2 )
		Local angle:Float = ATan2(-Self.y , Self.x)  - ATan2(-target.y , target.x)
		If angle < 0 Then angle += 360
		If angle >= 360 Then angle -= 360
		Return angle		
	End
	
	'-----------------------------------------------------------------------------------
	'		A N G L E   T O   C C W
	'-----------------------------------------------------------------------------------
	' If you have two vectors so they both start at the same position
	' returns the angle fromt this vector to target vector (in degrees)
	' if you where to go AntiClockwise to it, result is 0 to 360	
	'-----------------------------------------------------------------------------------
	
	Method AngleToAntiClockwise:Float( target:TVector2 )
		Local angle:Float =  AngleToClockwise(target)-360
		Return -angle
	End	

	
	'-----------------------------------------------------------------------------------
	' 		T O   S T R I N G
	'-----------------------------------------------------------------------------------
	Method ToString:String(clean:Bool=True) 
		Local res:String
		If clean Then
			res = Self.x+","+Self.y
		Else
			res = "("+Self.x+","+Self.y+")"
		End If
		Return res
	End 	
End

